const { Node, LinkedList } = require('./linkedList');
let linkedList = new LinkedList()
linkedList.append(1)
linkedList.append(2)
linkedList.append(3)
linkedList.append(4)
linkedList.append(5)

var reverseBetween = function (head, left, right) {
  // 创建一个虚拟头节点，方便处理从第一个节点开始反转的情况
  const dummy = new Node(-1);
  dummy.next = head;

  let pre = dummy; // 反转区间前一个节点
  // 将 pre 移动到 left 前一个节点
  for (let i = 0; i < left - 1; i++) {
    pre = pre.next;
  }

  let curr = pre.next; // 反转区间的第一个节点
  for (let i = 0; i < right - left; i++) {
    const next = curr.next;
    curr.next = next.next;
    next.next = pre.next;
    pre.next = next;
  }
  return dummy.next;
}
let rst = reverseBetween(linkedList.head, 2, 4)
console.log(JSON.stringify(rst));
